﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using CUREDataAccessLayers;

namespace CUREBusinessLogicLayer
{
    public class AuthorityBll
    {
        AuthorityDal authoritydal = new AuthorityDal();

        /// <summary>
        /// 获取用户组的详细信息
        /// </summary>
        /// <returns>用户组的详细信息</returns>
        public DataTable GetGroupData()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = authoritydal.GetGroupData();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return dt;
        }


        /// <summary>
        /// 新增用户组信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <returns>true 新增成功/false 新增失败</returns>
        public bool addGroupData(IDictionary<string, object> where)
        {
            bool jude = false;
            try
            {
                authoritydal.addGroupData(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return jude;

        }

        /// <summary>
        /// 更新用户组信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <returns>true 更新成功/false 更新失败</returns>
        public bool UpdateGroupData(IDictionary<string, object> where)
        {
            bool jude = false;
            try
            {
                authoritydal.UpdateGroupData(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return jude;
        }

        /// <summary>
        /// 删除用户组信息
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <returns>true 删除成功/false 删除失败</returns>
        public bool DeleteGroupData(IDictionary<string, object> where)
        {
            bool jude = false;
            try
            {
                authoritydal.DeleteGroupData(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return jude;
        }

        /// <summary>
        /// 根据组的info获取到组的name
        /// </summary>
        /// <param name="info">组info</param>
        /// <returns>组name</returns>
        public string GetGroupNameByInfo(string info)
        {
            string groupname = string.Empty;
            try
            {
                groupname = authoritydal.GetGroupNameByInfo(info);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return groupname;
        }

        /// <summary>
        /// 根据组的info获取到组的id
        /// </summary>
        /// <param name="id">组id</param>
        /// <returns>组name</returns>
        public string GetGroupNameByID(string id)
        {
            string groupname = string.Empty;
            try
            {
                groupname = authoritydal.GetGroupNameByID(id);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return groupname;
        }


        /// <summary>
        /// 获取模块列表
        /// </summary>
        /// <returns>模块信息</returns>
        public DataTable getActionList()
        {
            DataTable dt = new DataTable();
            try
            {
                dt = authoritydal.getActionList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }


        /// <summary>
        /// 根据用户组info获取已经配置的权限
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable getActionStrByGroupInfo(IDictionary<string, object> where)
        {
            DataTable dt = default(DataTable);
            try
            {
                dt = authoritydal.getActionStrByGroupInfo(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;


        }

        /// <summary>
        /// 根据用户组info获取已经配置的pda权限
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public DataTable getActionStrByGroupInfoPDA(IDictionary<string, object> where)
        {
            DataTable dt = default(DataTable);
            try
            {
                dt = authoritydal.getActionStrByGroupInfoPDA(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            return dt;
        }

        /// <summary>
        /// 修改用户组的权限
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <returns>true 修改成功/false 修改失败</returns>
        public bool EditGroupAuthority(IDictionary<string, object> where)
        {
            bool judge = false;
            try
            {
                judge = authoritydal.EditGroupAuthority(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return judge;
        }

        /// <summary>
        /// 修改用户组的pda权限
        /// </summary>
        /// <param name="where">字典参数</param>
        /// <returns>true 修改成功/false 修改失败</returns>
        public bool EditGroupAuthorityPDA(IDictionary<string, object> where)
        {
            bool judge = false;
            try
            {
                judge = authoritydal.EditGroupAuthorityPDA(where);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }
            return judge;
        }


        /// <summary>
        /// 获取菜单信息
        /// </summary>
        /// <param name="groupid">用户角色</param>
        /// <returns></returns>
        public string getFatherUrl(string groupid)
        {
            string html = "";
            DataTable dt = new DataTable();
            dt = authoritydal.getFatherUrl();
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    html += "<div class=\"accordionHeader\">"
                        + "<h2><span>Folder</span>" + dt.Rows[i]["action_columnname"].ToString().Trim() + "</h2></div>";
                    html += "<div class=\"accordionContent\">";
                    if (dt.Rows[i]["action_columnid"].ToString() != "")
                    {
                        html += getChildUrl(groupid, dt.Rows[i]["action_columnid"].ToString().Trim());
                    }
                    html += "</div>";
                }
            }
            return html;
        }

        /// <summary>
        /// 获取模块信息
        /// </summary>
        /// <param name="groupid">用户角色</param>
        /// <param name="columnid">菜单id</param>
        /// <returns></returns>
        public string getChildUrl(string groupid, string columnid)
        {

            string html = "";
            Dictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("groupid", groupid);
            dictionary.Add("columnid", columnid);
            DataTable dt = new DataTable();
            if (columnid != null && columnid != "")
            {
                html += "<ul class=\"tree treeFolder expand\">";
                dt = authoritydal.getChildUrl(dictionary);
                if (dt != null && dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        html += "<li><a href=\"" + dt.Rows[i]["action"].ToString().Trim() + "\" target=\"navTab\" rel=\"" + dt.Rows[i]["action"].ToString().Trim() + "\">" + dt.Rows[i]["action_name"].ToString() + "</a></li>";
                    }
                }
            }
            html += "</ul>";
            return html;
        }


    }
}
